<?php

namespace catchAdmin\system\controller;

use catcher\CatchResponse;
use catcher\base\CatchRequest;
use catcher\base\CatchController;
use app\common\model\OverallConfig;
use catchAdmin\system\request\OverallSituationRequest;

class OverallSituation extends CatchController
{
    /**
     * 获取全局配置
     * @return \think\response\Json
     */
    public function index(): \think\response\Json
    {
        $ret = (new OverallConfig())->getConditionData([],'id,name,describe,notes,status,sort,flag,parameter');
        return CatchResponse::success($ret);
    }

    /**
     * 新增
     * @param OverallSituationRequest $request
     * @return \think\response\Json
     */
    public function save(OverallSituationRequest $request): \think\response\Json
    {
        $req = $request->post();
        if (isset($req['status']) && !empty($req['status'])) {
            $req['status'] = (bool)$req['status'];
        }else{
            $req['status'] = false;
        }
        $req['sort'] = (int)$req['sort'];
        $req['deleted_at'] = 0;
        return CatchResponse::success((new OverallConfig())->addData($req));
    }

    /**
     * 更新
     * @param string $id 主键
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function update(string $id, CatchRequest $request): \think\response\Json
    {
        $req = $request->post();
        if (isset($req['status']) && !empty($req['status'])) {
            $req['status'] = (bool)$req['status'];
        }
        if (isset($req['sort']) && !empty($req['sort'])) {
            $req['sort'] = (int)$req['sort'];
        }
        return CatchResponse::success((new OverallConfig())->saveData($id, $req));
    }

    /**
     * 删除
     * @param string $id
     * @return \think\response\Json
     */
    public function delete(string $id): \think\response\Json
    {
        return CatchResponse::success((new OverallConfig())->destroy($id));
    }
}
